<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Hobby extends BaseModel
{
    protected $table = 'hobby';
    protected $table_content = 'hobby_content';
    protected $table_user = 'hobby_user';

    public $timestamps = true;

    public function getList(Request $request){
        $limit = $request->input('limit',10);
        $page = $request->input('page',1);
        $uuid = $request->input('uuid','');
        $hobby_id = $request->input('hobby_id','');
        $user_name = $request->input('user_name','');
        $un_id = $request->input('un_id','');
        $gender = $request->input('gender','');

        if($uuid || $user_name){
            $field = isset($uuid) ? 'uuid': 'nickname';
            $value = isset($uuid)?$uuid:$user_name;
            $user = DB::connection('api')->table('user')->where($field,$value)->select('user_id')->first();
            $user_id = $user->user_id;
        }

        $query = DB::connection('api')->table($this->table_content)->where('type',1)->where('status',1)
            ->leftJoin('union_user',$this->table_content . '.target_id','union_user.user_id')
            ->select($this->table_content . '.*','union_user.un_id');

        if (isset($user_id))$query->where($this->table_content.'.target_id',$user_id);
        if ($hobby_id)$query->where($this->table_content.'.hobby_id',$hobby_id);
        if ($gender)$query->where($this->table_content.'.gender',$gender);
        if($un_id)$query->where('union_user.un_id',$un_id);

        $data = $query->paginate($limit, '*','page',$page);
        if (!$data->toArray()['data']){
            return $data;
        }
        $user_ids =  array_unique(array_column($data->toArray()['data'],'target_id'));
        $users = (new Users())->getUserInfoByIds($user_ids);
        $users = array_column($users,null,'user_id');
        $hobby = $this->getType();
        $hobby = array_column($hobby,null,'id');

        $un_ids =  array_filter(array_unique(array_column($data->toArray()['data'],'un_id')));
        $union = (new Union())->getUnionByUnIds($un_ids);
        $union = array_column($union,null,'id');

        foreach ($data as $row){
            $row->nickname = $users[$row->target_id]->nickname??'';
            $row->uuid = $users[$row->target_id]->uuid??'';
            $row->hobby = $hobby[$row->hobby_id]->name??'';
            $row->date = round((time() - strtotime($row->created_time)) / 86400)??0;
            $row->union_name = $union[$row->un_id]->name??'';
        }
        return $data;


    }
    public function getType(){
        return DB::connection('api')->table($this->table)->get()->toArray();
    }

    //更新
    public function addOrupdateData($request)
    {
        $user_id = $request->input('user_id');
        $hobby_id = $request->input('hobby_id');
        $id = $request->input('id');
        $userArr = explode(',',$user_id);
        $res = [
            'code'  => 0,
            'msg'   => '成功',
            'data'  => []
        ];
        foreach ($userArr as $item){
            $data = [
                'id' => $id,
                'user_id' => $item,
                'hobby_id' => $hobby_id
            ];
            $curlRes = curl_request(getKkaudioApiUrl() . '/hobby/recommend/addOrUpdateRecommend', $data, 'json', 'POST');
            if ($curlRes['code'] != 0){
                $res['code'] = 400;
                $res['msg'] = '失败';
                array_push($res['data'],$curlRes['data']);
            }
        }
        return $res;
    }
    //删除
    public function delData($request)
    {
        $id = $request->input('id');

        $data = [
            'id'=> $id
        ];
        return curl_request(getKkaudioApiUrl() . '/hobby/recommend/deleteRecommend', $data, 'json', 'POST');
    }
    public function getTypeList(){
        return DB::connection('api')->table($this->table)->orderBy('sort','desc')->get(['id','name']);
    }

}
